package com.kkm.util;

import com.qiniu.util.Auth;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class QiNiuDown {

    private static String accessKey = "4PZCorU3u2Od-UEA8D8WHXSAn1t1Apj-dgzFt2wj";
    private static String secretKey = "lcwcOyIIn9itQCT27jvMJmSl_4Esvjs80Qzn3lP4";
    private static String bucket = "feiyumusic";

    public static Auth getAuth() {
        return Auth.create(accessKey, secretKey);
    }

    /**
     * 公有空间返回文件URL
     * @param fileName
     * @param domainOfBucket
     * @return
     */
    public static String publicFile(String fileName,String domainOfBucket) {
        String encodedFileName=null;
        try {
            encodedFileName = URLEncoder.encode(fileName, "utf-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String finalUrl = String.format("%s/%s", domainOfBucket, encodedFileName);
        System.out.println(finalUrl);
        return finalUrl;
    }

    /**
     * 私有空间返回文件URL
     * @param auth
     * @param fileName
     * @param domainOfBucket
     * @param expireInSeconds
     * @return
     */
    public static String privateFile(Auth auth,String fileName,String domainOfBucket,long expireInSeconds) {
        String encodedFileName=null;
        try {
            encodedFileName = URLEncoder.encode(fileName, "utf-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String publicUrl = String.format("%s/%s", domainOfBucket, encodedFileName);
        String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
        System.out.println(finalUrl);
        return finalUrl;
    }

//    public static void main(String[] args) {
//        publicFile("Beyond-光辉岁月.mp3","qzzqmzsyw.hb-bkt.clouddn.com");
//        //privateFile(CredentialsManager.getAuth(),"文件名","你的domainOfBucket",3600);
//    }
}
